package org.flexlancer.treeitemrenderer
{
	import mx.controls.Image;
	import mx.controls.treeClasses.TreeItemRenderer;
	import mx.controls.treeClasses.TreeListData;
	
	import org.flexlancer.enums.AppVariables;

	public class ElementTreeItemRenderer extends TreeItemRenderer
	{
		private var imgRTIcon:Image;
		private var lbl_x:Number=-9999;
		
		public function ElementTreeItemRenderer()
		{
			super();
			this.imgRTIcon = new Image();
			this.imgRTIcon.width = 20;
			this.imgRTIcon.height = 20;
			this.imgRTIcon.maintainAspectRatio = false;
		}

		override protected function createChildren():void{
			try{
				super.createChildren();
				this.addChild(this.imgRTIcon);
			}catch(err:Error){
				throw err;
			}
		}
		
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			try{
				var treeListData:TreeListData = TreeListData(this.listData);
				super.updateDisplayList(unscaledWidth,unscaledHeight);

				if ( this.icon != null )
					this.icon.visible = false;
				
				if ( this.lbl_x == -9999 ){
					this.lbl_x = this.label.x;
				}
				
				if ( treeListData.hasChildren ){
					this.imgRTIcon.visible = false;
					this.setStyle("fontWeight","bold");
					this.label.x = this.lbl_x;
				}else{
					if ( this.data.@isBranch == "true" )
						return;
					this.imgRTIcon.visible = true;
					this.setStyle("fontWeight","normal");
					
					var dta:XML = new XML(this.data);
					
					if ( dta.localName() == "Element" ){
						if ( this.data.@enabled == "true"){
							this.setStyle("color","black");
						}else{
							this.setStyle("color","red");
						}
					}
					this.imgRTIcon.x = lbl_x;
					this.label.x = this.lbl_x + 30;
					this.imgRTIcon.source = AppVariables.URL_GET_THUMBNAIL + "?id=" + this.data.@ThumbnailID;
				}
			}catch(err:Error){
				throw err;
			}
		}		
		
	}
}